package com.example.goodog.adapters;

import java.util.Date;
import java.util.Locale;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.goodog.ComparatorByAge;
import com.example.goodog.ComparatorByDistance;
import com.example.goodog.ComparatorByName;
import com.example.goodog.R;
import com.example.goodog.Utils;
import com.example.goodog.items.DogItem;

public class DogAdapter extends ItemAdapter{
	
	public DogAdapter(Context c, int PageNumber) {
		super(c, DogItem.class.getSimpleName(), R.layout.dog_item, PageNumber);
		
		AddComparator(new ComparatorByDistance(0, 0));
		AddComparator(new ComparatorByName());
		AddComparator(new ComparatorByAge());
	
	}
	

	public View getView(int position, View convertView, ViewGroup parent) {
		View List = super.getView(position, convertView, parent);
		DogItem item = ((DogItem)items.get(position));
		
		TextView DogNameText = (TextView)List.findViewById(R.id.dogNameTextView);
		DogNameText.setText(item.Name);
		

		
		ColorFilter Colorfilter = new LightingColorFilter( Color.BLUE, Color.BLUE );
		
		((ImageView)List.findViewById(R.id.imageView_xsmall)).setImageResource(R.drawable.xsmall);
		((ImageView)List.findViewById(R.id.imageView_small)).setImageResource(R.drawable.small);
		((ImageView)List.findViewById(R.id.imageView_medium)).setImageResource(R.drawable.medium);
		((ImageView)List.findViewById(R.id.imageView_large)).setImageResource(R.drawable.large);
		((ImageView)List.findViewById(R.id.imageView_xlarge)).setImageResource(R.drawable.xlarge);
		((ImageView)List.findViewById(R.id.imageView_xxlarge)).setImageResource(R.drawable.xxlarge);
		
		ImageView SelectedImage = (ImageView)List.findViewById(R.id.imageView_xsmall);
		Drawable myIcon =  context.getResources().getDrawable( R.drawable.xsmall_color );
		
		switch (item.Size) {
		case 1:
			SelectedImage = (ImageView)List.findViewById(R.id.imageView_xsmall);
			myIcon =  context.getResources().getDrawable( R.drawable.xsmall_color );
			break;
		case 2:
			SelectedImage = (ImageView)List.findViewById(R.id.imageView_small);
			myIcon =  context.getResources().getDrawable( R.drawable.small_color );
			break;
		case 3:
			SelectedImage = (ImageView)List.findViewById(R.id.imageView_medium);
			myIcon =  context.getResources().getDrawable( R.drawable.medium_color );
			break;
		case 4:
			SelectedImage = (ImageView)List.findViewById(R.id.imageView_large);
			myIcon =  context.getResources().getDrawable( R.drawable.large_color );
			break;
		case 5:
			SelectedImage = (ImageView)List.findViewById(R.id.imageView_xlarge);
			myIcon =  context.getResources().getDrawable( R.drawable.xlarge_color );
			break;
		case 6:
			SelectedImage = (ImageView)List.findViewById(R.id.imageView_xxlarge);
			myIcon =  context.getResources().getDrawable( R.drawable.xxlarge_color );
			break;
		}
		
		myIcon.setColorFilter(Colorfilter);
		SelectedImage.setImageDrawable(myIcon);
		
		
		
		if(item.Sex)
		{
			((ImageView)List.findViewById(R.id.imageView_sex)).setImageResource(R.drawable.male);
		}
		else
		{
			((ImageView)List.findViewById(R.id.imageView_sex)).setImageResource(R.drawable.female);
		}

		
		ImageView DogImage = (ImageView) List.findViewById(R.id.dogImage);
		Bitmap bmp = item.getBitmap();
		if(bmp != null)
		{
			DogImage.setImageBitmap(bmp);
		}
		
		TextView DogAgeText = (TextView)List.findViewById(R.id.textViewDogAge);
		Date DogBirthDate = Utils.StringToDate(item.DateOfBirth, "MM/yy");
        Date now = new Date();
        int monthDiff = Utils.monthsBetween(now,DogBirthDate);
        DogAgeText.setText(String.format("Age: %d,%d",monthDiff/12,monthDiff%12));
	
		
		AddButtonsToToolbar(List, item);
	
		
		return List;
	}		
	
	public String GetTitle()
	{
		Locale l = Locale.getDefault();
		return context.getString(R.string.dogs).toUpperCase(l);
	}

}
